package com.swe727.spring2011.constraint1.client.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class LogUtil {
	
	public static String ROOT_DIR;
	
	public static void writeMedevac(String nodeId, long latency) {
		
	}
	
	public static void writeNodeRegisterLatency(String nodeId, long latency) {
		writeLog(nodeId, "registerLatency=" + latency);
	}
	
	public static void writeReceiveMedevacLatency(String nodeId, long latency) {
		writeLog(nodeId + "-medevac", "receiveMedevacLatency=" + latency);
	}
	
	public static void writeReceivedMedevacCount(String nodeId, long medevacCount) {
		writeLog(nodeId, "numReceivedMedvacs=" + medevacCount);
	}
	
	public static void writeRequestedMedevacCount(String nodeId, long medevacCount) {
		writeLog(nodeId, "numRequestedMedvacs=" + medevacCount);
	}
	
	public static void writeAverageGetLatency(String nodeId, double latency) {
		writeLog(nodeId, "averageGetObsLatency=" + latency);
	}
	
	public static void writeMemoryUsage(String nodeId) {
		long freeMemory = Runtime.getRuntime().freeMemory();
		long totalMemory = Runtime.getRuntime().totalMemory();
		
		writeLog(nodeId + "-memory", "memory=" + freeMemory + "/" + totalMemory);
	}
	
	public static void writeLog(String nodeId, String msg) {
		try {
			msg = msg + "\n";
			FileOutputStream fos = new FileOutputStream(ROOT_DIR +"/node-" + nodeId + ".log", true);
			fos.write(msg.getBytes());
			fos.close();
		} catch (FileNotFoundException ex) {
			System.out.println("FileNotFoundException : " + ex);
		} catch (IOException ioe) {
			System.out.println("IOException : " + ioe);
		}
	}

}
